﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace uTILLIty.Rave.Core
{
	public class ExpectedExceptionMessageAttribute : ExpectedExceptionBaseAttribute
	{
		public ExpectedExceptionMessageAttribute(Type expectedType, string expectedMessage)
		{
			Init(expectedType, expectedMessage);
		}

		public ExpectedExceptionMessageAttribute(Type expectedType, string expectedMessage, string noExceptionMessage)
			: base(noExceptionMessage)
		{
			Init(expectedType, expectedMessage);
		}

		private void Init(Type exceptionType, string expectedMessage)
		{
			if (exceptionType == null)
				throw new ArgumentNullException("exceptionType");
			if (expectedMessage == null)
				throw new ArgumentNullException("expectedMessage");

			ExpectedType = exceptionType;
			ExpectedMessage = expectedMessage;
		}

		public Type ExpectedType { get; set; }
		public string ExpectedMessage { get; set; }

		protected override void Verify(Exception exception)
		{
			var actualType = exception.GetType();
			if (!actualType.Equals(ExpectedType))
				throw new ApplicationException(string.Format("Expected type {0}, but was type {1}", ExpectedType, actualType));

			var actualMessage = exception.Message;
			if (!ExpectedMessage.Equals(actualMessage, StringComparison.CurrentCultureIgnoreCase))
				throw new ApplicationException(string.Format("Expected message '{0}', but was '{1}'", ExpectedMessage, actualMessage));
		}
	}
}